package com.szholly.data.coderule.table;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.data.general.*;
import com.szholly.data.general.metadata.IBaseTable;

/**
 * 编码规则信息表
 */
@Component("DATA_RULEINFO")
public class RuleInfoTable extends InterceptorTableBase implements IBaseTable {

	public final static String TABLE_NAME = "DATA_RULEINFO";
	private HashMap<String, String> ruleType = new LinkedHashMap<String, String>();
	private HashMap<String, String> fillType = new LinkedHashMap<String, String>();

	/**
	 * 获取单例对象
	 */
	public static RuleInfoTable getSingleRef() {
		return (RuleInfoTable) SpringBeanFactory.getBean("DATA_RULEINFO");
	}

	@Override
	public String getMetadataTableName() {
		return TABLE_NAME;
	}

	@Override
	public String getTableGroup() {
		return "编码规则";
	}

	@Override
	public String getMetadataTableAliasName() {
		return "规则信息主表";
	}

	private HashMap<String, Field> fields;

	@Override
	protected HashMap<String, Field> fields() {
		if (null == ruleType) {
			ruleType = new LinkedHashMap<String, String>();
		}
		if (null == fillType) {
			fillType = new LinkedHashMap<String, String>();
		}
		if (fields == null) {
			fields = new HashMap<String, Field>();

			Field itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_GUID);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(1);
			itemField.setFieldAliasName("全局ID");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_VERSION);
			itemField.setDataType(GlobalDataType.Int32);
			itemField.setFieldID(2);
			itemField.setFieldAliasName("数据版本");
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_COMPANY);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(3);
			itemField.setFieldAliasName("企业名称");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_SYSTEMNAME);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(3);
			itemField.setFieldAliasName("系统名称");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(RuleInfoRow.FIELD_ID);
			itemField.setDataType(GlobalDataType.Int32);
			itemField.setFieldID(2);
			itemField.setFieldAliasName("");
			itemField.setCanShow(false);
			itemField.setAllowNull(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(RuleInfoRow.FIELD_RULEID);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(3);
			itemField.setFieldAliasName("");
			itemField.setCanShow(false);
			itemField.setAllowNull(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(RuleInfoRow.FIELD_ORDERBY);
			itemField.setDataType(GlobalDataType.Int32);
			itemField.setFieldID(4);
			itemField.setFieldAliasName("编码顺序");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(RuleInfoRow.FIELD_TYPE);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(5);
			itemField.setFieldAliasName("编码类型");
			ruleType.put("1", "最大值");
			ruleType.put("2", "最小值");
			ruleType.put("3", "最小未使用值");
			ruleType.put("4", "最大未使用值");
			ruleType.put("5", "模糊查询最大值");
			ruleType.put("6", "模糊查询最小值");
			ruleType.put("7", "模糊查询最小未使用值");
			ruleType.put("8", "模糊查询最大未使用值");
			ruleType.put("9", "序列值");
			ruleType.put("10", "字段值");
			ruleType.put("11", "固定值");
			ruleType.put("12", "当前日期值");
			IDomain pDomain = new MemDomain(ruleType, "(编码规则)编码类型");
			itemField.setDomain(pDomain);
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(RuleInfoRow.FIELD_PARAS);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(6);
			itemField.setFieldAliasName("编码参数");
			itemField.setCanShow(false);
			itemField.setAllowNull(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(RuleInfoRow.FIELD_LENGHT);
			itemField.setDataType(GlobalDataType.Int32);
			itemField.setFieldID(7);
			itemField.setFieldAliasName("编码长度");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(RuleInfoRow.FIELD_FILLCHAR);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(8);
			itemField.setFieldAliasName("填充字符");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(RuleInfoRow.FIELD_FILLTYPE);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(9);
			itemField.setFieldAliasName("填充类型");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fillType.put("0", "左填充");
			fillType.put("1", "右填充");
			pDomain = new MemDomain(fillType, "(编码规则)填充类型");
			itemField.setDomain(pDomain);

			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(RuleInfoRow.FIELD_NEEDFIELDS);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(10);
			itemField.setFieldAliasName("编码相关字段");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
		}
		return fields;
	}

	private static Map<String, ArrayList<RuleInfoRow>> convertTables;

	public static void InitFiles(RuleInfoTable convertInfoTable) {
		if (convertTables == null) {
			convertTables = new HashMap<String, ArrayList<RuleInfoRow>>();

			IQueryFilter queryFilter = new QueryFilter();
			queryFilter.setOrderByString(" I_ORDERBY ");
			List<RowBase> rows = (List<RowBase>) getSingleRef().getEntityList(RowBase.class, 
					queryFilter);

			for (RowBase row : rows) {
				String key = ((RuleInfoRow) row).getRULEID();
				if (!convertTables.containsKey(key)) {
					convertTables.put(key, new ArrayList<RuleInfoRow>());
				}
				convertTables.get(key).add((RuleInfoRow) row);
			}
		}
	}

	public List<RuleInfoRow> GetRowByRuleID(String id) {
		if (convertTables == null)
			InitFiles(this);

		if (id.contains("'"))
			id = id.substring(1, id.length() - 2);
		if (convertTables.containsKey(id))
			return convertTables.get(id);
		else
			return null;
	}
}
